<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfos.query" clearable @clear="getGoodLists">
            <el-button slot="append" icon="el-icon-search" @click="getGoodLists"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodLists" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="80px" label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column width="80px" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="155px" label="创建时间">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column width="190px" label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handelEditGoods(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页，页码区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfos.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfos.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 发请求请求商品的参数
      queryInfos: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品的数据
      goodLists: [],
      // 总条数
      total: ''
    }
  },
  methods: {
    async getGoodLists () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfos
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品信息失败')
      }
      this.goodLists = res.data.goods
      this.total = res.data.total
    },
    // 当页码改变的时候触发这个处理函数
    handleSizeChange (newSize) {
      this.queryInfos.pagesize = newSize
      this.getGoodLists()
    },
    // 当切换页的触发这个事件
    handleCurrentChange (newPage) {
      this.queryInfos.pagenum = newPage
      this.getGoodLists()
    },
    // 根据id删除商品
    async removeById (id) {
      const confirmResult = await this.$messageBox
        .confirm('此操作将永久删除此商品？', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      this.$message.success('删除商品成功')
      this.getGoodLists()
    },
    // 跳转到添加商品页面
    goAddGoodsPage () {
      this.$router.push('/goods/add')
    }
  },
  created () {
    this.getGoodLists()
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
</style>
